package com.aproj.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;

import com.aproj.dao.UserDao;
import com.aproj.model.Role;
import com.aproj.model.User;
import com.aproj.model.UserRole;

public class UserLocalServiceUtil {
	
	private static UserDao userDao;

	public static UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserServiceUtil userDao) {
		UserLocalServiceUtil.userDao = userDao.getUserDao();
	}
	
	public static User Authenticate(String email, String password){
		
		User user = userDao.getUserByEmailAndPassword(email, password);
		
		if(user == null)return null;
		
		List<UserRole> userRoles = UserRoleLocalServiceUtil.getUserRoles(user.getUserId());
		
		List<Role> roles = new ArrayList<Role>();
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		for(UserRole ur: userRoles){
			roles.add(ur.getRole());
		}
		
		user.setRoles(roles);
		
		return user;
	}
	
	

}
